package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysMessage;
import com.ruoyi.system.service.ISysMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.enums.RoleEnums;

/**
 * 消息通知Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/message")
public class SysMessageController extends BaseController
{
    @Autowired
    private ISysMessageService messageService;

    /**
     * 查询消息通知列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysMessage message)
    {
        // 获取当前登录用户
        Long userId = SecurityUtils.getUserId();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String receiverType = "1"; // 默认为老人类型
        // 管理员可查看所有消息
        if (loginUser.getUser().isAdmin()){
            message.setReceiverId(null);
            message.setReceiverType(null);
        }else {
            // 如果是老人角色，只能查看自己的消息
            message.setReceiverId(userId);
            if (SecurityUtils.hasRole(RoleEnums.elder.getValue())) {
                // 老人
                message.setReceiverType("1");
            }
            // 如果是家属角色，只能查看自己的消息
            else if (SecurityUtils.hasRole(RoleEnums.family.getValue())) {
                // 家属
                message.setReceiverType("2");
            }else if (SecurityUtils.hasRole(RoleEnums.nurse.getValue())) {
                // 护理员
                message.setMessageType("3");
                message.setReceiverType("3");
            }
        }
        
        startPage();
        List<SysMessage> list = messageService.selectSysMessageList(message);
        return getDataTable(list);
    }

    /**
     * 导出消息通知列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:export')")
    @Log(title = "消息通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMessage message)
    {
        List<SysMessage> list = messageService.selectSysMessageList(message);
        ExcelUtil<SysMessage> util = new ExcelUtil<SysMessage>(SysMessage.class);
        util.exportExcel(response, list, "消息通知数据");
    }

    /**
     * 获取消息通知详细信息
     */
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId)
    {
        return success(messageService.selectSysMessageByMessageId(messageId));
    }

    /**
     * 新增消息通知
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @Log(title = "消息通知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMessage message)
    {
        return toAjax(messageService.insertSysMessage(message));
    }

    /**
     * 修改消息通知
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "消息通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMessage message)
    {
        return toAjax(messageService.updateSysMessage(message));
    }

    /**
     * 删除消息通知
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "消息通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds)
    {
        return toAjax(messageService.deleteSysMessageByMessageIds(messageIds));
    }
    
    /**
     * 标记消息为已读
     */
    @Log(title = "消息通知", businessType = BusinessType.UPDATE)
    @PutMapping("/read/{messageId}")
    public AjaxResult markAsRead(@PathVariable Long messageId)
    {
        return toAjax(messageService.markAsRead(messageId));
    }
    
    /**
     * 获取未读消息数量
     */
    @GetMapping("/unread/count")
    public AjaxResult getUnreadCount()
    {
        // 获取当前登录用户
        Long userId = SecurityUtils.getUserId();
        String receiverType = "1"; // 默认为老人类型
        
        // 判断用户角色
        if (SecurityUtils.hasRole(RoleEnums.family.getValue())) {
            receiverType = "2"; // 家属
        }
        
        int count = messageService.getUnreadCount(userId, receiverType);
        return AjaxResult.success(count);
    }
} 